package SocialMediaSentiment;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class calcshit {

	public static void main(String[] arg) throws IOException {
		float[] newbounds = calcBounderies(0.27f,-0.5f);
		
	}
		
	public static float[] calcBounderies(float startupper, float startlower) throws IOException{
		HashMap<String, Integer> trigrams = TweetNGram.getTriGrams();
		Map<String, Double> woordenweightslijst = StandardFunctions.getWeightsListOLD(0);
		float[] bounderies = new float[2];
		double besterror = 100;		
		float stepsize = 0.01f;
		float bestupper = startupper;
		float bestlower = startlower;
		double uppererror, lowererror;
		
		besterror = calcTrigramValues.calcLimits(trigrams, bestupper, startlower, woordenweightslijst);
		while(stepsize < 1)
		{
			uppererror = calcTrigramValues.calcLimits(trigrams, bestupper + stepsize, startlower, woordenweightslijst);
			lowererror = calcTrigramValues.calcLimits(trigrams, bestupper - stepsize, startlower, woordenweightslijst);
			if( uppererror < besterror && uppererror <= lowererror )
			{
				bestupper = bestupper + stepsize;
				besterror = uppererror;
				 stepsize = 0.01f;
			}
			else if( lowererror < besterror && lowererror < uppererror )
			{
				bestupper = bestupper - stepsize;
				besterror = lowererror;
				 stepsize = 0.01f;
			}
			else 
				stepsize = stepsize * 2;
		}
		System.out.println("Best error: " + besterror + " bestupper:  " + bestupper);
		bounderies[0] = bestupper;
		stepsize = 0.01f;
		while(stepsize < 1)
		{
			uppererror = calcTrigramValues.calcLimits(trigrams, bestupper, bestlower  + stepsize, woordenweightslijst);
			lowererror = calcTrigramValues.calcLimits(trigrams, bestupper, bestlower - stepsize, woordenweightslijst);
			if( uppererror < besterror && uppererror <= lowererror )
			{
				bestlower = bestlower + stepsize;
				besterror = uppererror;
				 stepsize = 0.01f;
			}
			else if( lowererror < besterror && lowererror < uppererror )
			{
				bestlower = bestlower - stepsize;
				besterror = lowererror;
				 stepsize = 0.01f;
			}
			else 
				stepsize = stepsize * 2;
		}
		System.out.println("Best error: " + besterror + " bestlower:  " + bestlower);
		bounderies[1] = bestlower;
		
		return bounderies;		

	}

}

